package shuishijie.suishijie.model.web;




import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import shuishijie.suishijie.model.exception.ExceptionEngine;
import shuishijie.suishijie.model.exception.ServerException;

/**
 * 网络数据统一处理
 * Administrator on 2016/8/10.
 */
public class WebManager {


    public static final String Error = "error";

    public static <T extends BaseResponse> ObservableTransformer<T, T> allTransformer() {

        return responseObservable -> responseObservable.map(tResponse -> {
            if (!tResponse.isSuccess()) {
                throw new ServerException(tResponse.code, tResponse.msg);
            }
            return tResponse;
        }).onErrorResumeNext(new WebManager.HttpResponseFunc<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static <T extends BaseResponse> ObservableTransformer<T, T> sTransformer() {

        return responseObservable -> responseObservable.map(tResponse -> {
            if (!tResponse.isSuccess()) {
                throw new ServerException(tResponse.code, tResponse.msg);
            }
            return tResponse;
        }).onErrorResumeNext(new WebManager.HttpResponseFunc<>());
    }

    public static <T> ObservableTransformer<T, T> switchSchedulers() {
        return observable -> observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


//
//    private static class HttpResponseFunc<T> implements Func1<Throwable, Observable<T>> {
//        @Override
//        public Observable<T> call(Throwable throwable) {
//            //ExceptionEngine为处理异常的驱动器
//            return Observable.error(new Throwable(throwable));
//        }
//    }


    /**
     * 所有数据的统一处理
     *
     * @param dto
     * @param <T>
     * @return
     */
    public static <T extends BaseResponse> Observable<T> flatResult(T dto) {
        return Observable.create(subscriber -> {
            if (dto.isSuccess()) {
                subscriber.onNext(dto);
            } else {
                subscriber.onError(new ServerException(dto.code, dto.msg));
            }
            subscriber.onComplete();
        });
    }

    public static class HttpResponseFunc<T> implements Function<Throwable, Observable<T>> {
        @Override
        public Observable<T> apply(Throwable throwable) {
            //ExceptionEngine为处理异常的驱动器
            return Observable.error(ExceptionEngine.handleException(throwable));
        }
    }


//
//    public static <T> Observable<T> flatResult2(Throwable e) {
//        Log.i("zhangrl", "zhangrl: flatResult Exception ");
//        e.printStackTrace();
//        return Observable.create(subscriber -> {
//            subscriber.onError(new Throwable());
//        });
//
//    }


//    /**
//     * 设置列表的统一数据处理
//     *
//     * @param dto
//     * @param model
//     * @param <T>
//     * @return
//     */
//    public static <T extends BaseDto> Observable<T> flatResult(T dto, ListModel model) {
//        //设置总数
//        model.setTotalCount(dto.getTotalCount());
//        int page = model.getCurrentPage() + 1;
//        model.setCurrentPage(page);
//        //设置下一页开始位置
//        model.setNextStart(page * model.getLimit());
//        return flatResult(dto);
//    }

    /**
     * 处理错误信息
     *
     * @param throwable
     * @return
     */
    public static String setThrowable(Throwable throwable) {
        try {
            if (throwable.getMessage().equals(Error)) {
                return throwable.getCause().getMessage();
            } else {
                return "网络连接失败";
            }
        } catch (Exception ex) {
            return "网络连接失败";
        }

    }

}

